<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="info.title" v-model="visible" align-center width="500px" :z-index="2000" @close="handleClose" destroy-on-close>
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="选择合同模板" prop="ct_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.ct_id" placeholder="请输入" :type="info.type" filterable clearable class="w-full" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { nextTick, reactive } from 'vue';
  import api from '../api'
  import { ElMessage } from 'element-plus'
  const props = defineProps({
    options: { type: Object, default: () => ({}) },
  })
  const emit = defineEmits(['close', 'success'])
  const load = ref(false)
  const visible = ref(false)
  let loadings = reactive({})

  // 表单处理
  const formRef = ref()
  const form = ref({})
  const contract_obj = {
    1:{title:"生成开票合同",api:'addByBill',type:'contract3'},
    2:{title:"生成送货清单",api:'addByBillingDelivert',type:'contract5'},
    3:{title:"生成开票清单",api:'addByBillingList',type:'contract4'},
    4:{title:"生成开票明细",api:'addByBillingListDetail',type:'contract6'},
  }
  const info = ref({})
  // 打开弹窗
  const open = async (b_ids,type) => {
    info.value = contract_obj[type]
    if (!load.value) {
      load.value = true
      await 1
    }
    form.value = {
      ...b_ids
    }
    loadings = reactive({})
    visible.value = true
    formRef?.value?.resetFields()
  }
  // 关闭弹窗
  const close = () => {
    visible.value = false
  }

  const handleClose = () => {
    emit('close')
  }
  // 提交
  const create = async (formRef) => {
    if (!await formRef.validate()) return
    loadings.create = true
    api[info.value.api](form.value).then(res => {
      ElMessage.success('操作成功')
      emit('success')
      close()
    }).finally(() => {
      loadings.create = false
    })
  }

  defineExpose({ open })
</script>